package cn.flashk.controls {
	import com.skins.Skin;
	
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	import cn.flashk.controls.managers.DefaultStyle;
	import cn.flashk.controls.managers.SkinLoader;
	import cn.flashk.controls.managers.SkinManager;
	import cn.flashk.controls.managers.SourceSkinLinkDefine;
	import cn.flashk.controls.modeStyles.ButtonStyle;
	import cn.flashk.controls.modeStyles.TextInputStyle;
	import cn.flashk.controls.skin.ActionDrawSkin;
	import cn.flashk.controls.skin.TextInputSkin;
	import cn.flashk.controls.skin.sourceSkin.TextInputSourceSkin;
	import cn.flashk.controls.support.ColorConversion;
	import cn.flashk.controls.support.UIComponent;
	import cn.flashk.controls.support.XString;

	/**
	 * TextInput 组件是单行文本组件，其中包含本机 ActionScript TextField 对象。
	 *
	 * @langversion ActionScript 3.0
	 * @playerversion Flash 9.0
	 *
	 * @see cn.flashk.controls.support.UIComponent
	 *
	 * @author flashk
	 */
	public class TextInput extends UIComponent {
		protected var txt:TextField;
		protected var _tipText:String;
		protected var tf:TextFormat;
		
		/**设置文本颜色值*/
		protected var _color:Object;
		
		public function TextInput(skinID:String = ""){
			super(skinID);
			_compoWidth = 120;
			_compoHeight = 24;
			txt = new TextField();
			txt.background = false;
			txt.type = TextFieldType.INPUT;
			txt.x = 5;
			txt.y = 3;
			txt.height = 20;
			txt.addEventListener(FocusEvent.FOCUS_IN, checkTextFocusIn);
			txt.addEventListener(FocusEvent.FOCUS_OUT, checkTextFocusOut);
			this.addChild(txt);
			new TextInputStyle(styleSet);
			
			if(_skinID!=""||_skinID=="null"){
				tf = Skin.librarys.getTextFormat(Skin.librarys.sheet.getStyleValue(_skinID,"upTxt"));
			}
			if(tf==null){
				tf = new TextFormat();
				tf.align = TextFormatAlign.LEFT;
				tf.size = DefaultStyle.fontSize;
				tf.color = ColorConversion.transformWebColor(DefaultStyle.textColor);
				tf.font = DefaultStyle.font;
			}
			textFormat = tf;
			setSize(_compoWidth, _compoHeight);
			this.dispatchEvent(new Event(Event.INIT));
		}

		public function set textFormat(value:TextFormat):void{
			tf = value;
			_color = tf.color;
			txt.defaultTextFormat = value;
			if(txt.textHeight > _compoHeight)
				_compoHeight = txt.textHeight;
			setSize(_compoWidth,_compoHeight);
		}
		
		override protected function updateSkinBefore():void {
			styleSet[ButtonStyle.DEFAULT_SKIN_ELLIPSE_WIDTH] = DefaultStyle.ellipse;
			styleSet[ButtonStyle.DEFAULT_SKIN_ELLIPSE_HEIGHT] = DefaultStyle.ellipse;
			styleSet[ButtonStyle.DEFAULT_SKIN_ELLIPSE_BOTTOM_WIDTH] = DefaultStyle.ellipse;
			styleSet[ButtonStyle.DEFAULT_SKIN_ELLIPSE_BOTTOM_HEIGHT] = DefaultStyle.ellipse;
		}

		/**
		 * 设置最大可以输入的中文数，一个中文按照两个英文字母计算，所以如果设置为 10则可以输入10个中文，20个英文，如果从剪切板复制过来，则进行截断
		 * @param max
		 *
		 */
		public function set maxChineseChars(max:uint):void {
			XString.setInputMaxChars(txt, max * 2);
		}

		public function get textField():TextField {
			return txt;
		}

		public function set text(value:String):void {
			if (value == null)
				value = "";
			txt.text = value;
			if(_color){
				tf.color = _color;
				txt.setTextFormat(tf);
			}
		}

		public function get text():String {
			return txt.text;
		}

		public function set restrict(value:String):void {
			txt.restrict = value;
		}

		public function set tipText(value:String):void {
			_tipText = value;
			tf.color = styleSet[TextInputStyle.TIP_TEXT_COLOR];  //提示文本颜色
			txt.defaultTextFormat = tf;
			txt.text = _tipText;
			
			tf.color = _color;									 //设置文本颜色
			textFormat = tf;
		}

		override public function setDefaultSkin():void {
			setSkin(TextInputSkin);
		}

		override public function setSourceSkin():void {
			setSkin(SkinLoader.getClassFromSkinFile(SourceSkinLinkDefine.TEXT_INPUT));
		}

		override public function setLoaderSkin(skinID:String=""):void {
			var sous:TextInputSourceSkin = new TextInputSourceSkin(_skinID);
			sous.init(this, styleSet, null);
			skin = sous;
		}

		override public function setSkin(Skin:Class):void {
			if (SkinManager.isUseDefaultSkin == true){
				skin = new Skin();
				ActionDrawSkin(skin).init(this, styleSet);
			} else {
				var sous:TextInputSourceSkin = new TextInputSourceSkin();
				sous.init(this, styleSet, Skin);
				skin = sous;
			}
		}

		override public function setSize(newWidth:Number, newHeight:Number):void {
			super.setSize(newWidth, newHeight);
			txt.width = _compoWidth - 10;
			txt.height= 20;
			txt.y = _compoHeight > txt.textHeight?(_compoHeight - txt.textHeight)/2 - 2 : 0;
		}

		override public function setStyle(styleName:String, value:Object):void {
			super.setStyle(styleName, value);
			switch (styleName){
				case TextInputStyle.TEXT_COLOR:
					break;
			}
		}

		protected function checkTextFocusIn(event:FocusEvent):void {
			if (txt.text == _tipText){
				txt.text = "";
			}
		}

		protected function checkTextFocusOut(event:FocusEvent):void {
			if (txt.text == ""){
				if (_tipText != null && _tipText != ""){
					txt.text = _tipText;
					tf.color = styleSet[TextInputStyle.TIP_TEXT_COLOR];
					textFormat = tf;
				}
			}
		}
	}
}